<?php
/**
 * 
 */
class RedisPool   {
	
	private static $connections = array();
	private static $servers = array();
	private static $client;
	private static $sphinxClient;
	private static $amqPool;
	
	public static function add_servers( $list )
	{
		foreach ( $list as $alias => $data )
		{
			self::$servers[$alias] = $data;
		}
	}

	public static function get( $alias )
	{
		if ( !array_key_exists($alias, self::$connections) )
		{
			$redis = new Redis();
			$redis->connect(self::$servers[$alias][0],self::$servers[$alias][1]);
			self::$connections[$alias] = $redis;
		}
		if(isset(self::$servers[$alias][2]) && self::$servers[$alias][2]!="" ){
			self::$connections[$alias]->auth(self::$servers[$alias][2]);
		}
		return self::$connections[$alias];
	}
	// 初始化异步队列
	public static function initGearmanClient(){
	    self::$client  = new GearmanClient();
	    if(!empty(self::$client)){
	        self::$client->addServer("10.47.109.31", 4730);
	    }
	    AC::QUEUE_AMQ_STATUS && self::$amqPool = new AMQPool();
	}
	// 关注 时间线
	public static function addGearmanJob($do, $data, $keyId){
	    if (empty($do) || empty($data)) return false;
	
	    $json = json_encode($data);
	    if(!empty(self::$client)){
	        self::$client->doBackground($do, $json, $keyId);
	    }
	
	}
	
	// feed
	public static function addFeedGearmanJob($do, $data){
	    if (empty($do) || empty($data)) return false;
	    
	    if(!empty(self::$client)){
	       self::$client->doBackground($do, $data);
	    }
	
	}
	// Classify
	public static function addClassifyGearmanJob($do, $data){
	    if (empty($do) || empty($data)) return false;
	    
	    if(!empty(self::$client)){
	       self::$client->doBackground($do, $data);
	    }
	}

    public static function addDiaryReviewGearmanJob($do, $data){
        if (empty($do) || empty($data)) return false;
        $data = json_encode($data);
        if(!empty(self::$client)){
            self::$client->doBackground($do, $data);
        }

    }
	
	public static function addCollectionGearmanJob($do, $data){
	    if (empty($do) || empty($data)) return false;

		if(!empty(self::$amqPool)){
			self::$amqPool->publish($data, "h_log_collection", "hq_log_collection", "");
		}
	}
	
	public static function addDiaryTopicCreateJob($do, $data){
	    if (empty($do) || empty($data)) return false;

		if(!empty(self::$amqPool)) {
			self::$amqPool->publish($data, "h_web_diarytopic", "hq_web_diarytopic", "");
		}
	}

    public static function addChatMsg2IMCreateJob($do, $data){
	    return true;
        if (empty($do) || empty($data)) return false;

        if(!empty(self::$amqPool)) {
            self::$amqPool->publish($data, "h_im_chatmsg", "hq_im_chatmsg", "");
        }
    }

    public static function addUserEventCreateJob($do, $data){
        return true;
        if (empty($do) || empty($data)) return false;

        if(!empty(self::$amqPool)) {
            self::$amqPool->publish($data, "h_ff_event", "hq_ff_event", "");
        }
    }

	public static function addCountWeightJob($do, $data){
		if (empty($do) || empty($data)) return false;

		if(!empty(self::$amqPool)) {
			self::$amqPool->publish($data, "h_count_weight", "hq_count_weight", "");
		}
	}
	
	// Gearman函数列表：http://luhuang.sinaapp.com/gearman-gearmanclient/
	public static function doHighGearmanJob ($do, $data) {
	    if (empty($do) || empty($data)) return false;
	    
	    if(!empty(self::$client)){
	       return self::$client->doHigh($do, $data);
	    }
	}
	
	public static function doBackgroundGearmanJob ($do, $data, $keyId = '') {
	    if (empty($do) || empty($data)) return false;
	    
	    if(!empty(self::$client)){
	       self::$client->doBackground($do, $data, $keyId);
	    }
	}
	
	// 初始化sphinx搜索
	public static function initSphinx(){
	    try {
	        self::$sphinxClient    =   new SphinxClient();
    	    self::$sphinxClient->SetServer ( '10.160.6.159', 9312);
    	    self::$sphinxClient->SetConnectTimeout ( 3 );
    	    self::$sphinxClient->SetArrayResult ( true );
    	    self::$sphinxClient->SetMatchMode(SPH_MATCH_ALL);
    	    return self::$sphinxClient;
	    } catch ( Exception $e ) {
	    	UK::saveLog('Sphinx.log', 'throw:'.print_r($e->getMessage(),true)."\r\n", TRUE);
	        return null;
	    }
	    return null;
	}
}

class AMQPool {
	private $connection = null;

	function __construct() {
	    $this->connection = new AMQPConnection(array('host' =>'10.47.89.27', 'port' =>'5672', 'vhost' =>'pink_server', 'login' =>'pink_server', 'password' => '6Up9noEOpdG2'));
	    $this->connection->connect() or $this->connection = null;
	}

	function __destruct() {
	    if($this->connection != null){
                $this->connection->disconnect();
	    }
	}

	function publish($message, $exchangeName, $queueName, $routeKey) {
	    if(empty($message) || empty($exchangeName)){
			return;
	    }
		try {
			$channel = new AMQPChannel($this->connection);
			$exchange = new AMQPExchange($channel);
			$exchange->setName($exchangeName);
			$queue = new AMQPQueue($channel);
			$queue->setName($queueName);
			$queue->bind($exchangeName, $queue->getName());
			$exchange->publish($message, $routeKey);
		}catch (AMQPConnectionException $e) {
	    	UK::saveLog('AMQPConnectio.error.log', print_r($e, true)."\r\n", TRUE);
		}
	}
}
